package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.entity.web.WebUserCollect;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

/**
 * 收藏夹接口
 * @author Administrator
 */
@Service
public class CollectServiceImpl extends BaseService {


  /**
   * 获取收藏夹列表
   *
   * @param form 查询语句
   * @return
   */
  @Override
  public JSONObject getList(QueryForm form) {
    String sql = "select " +
        "a.id," +
        "a.productId," +
        "a.createDate," +
        "b.name," +
        "b.shoppePrice " +
        " from " +
        " web_user_collect a," +
        " product b " +
        " where a.productId=b.id  ";
    String countSql = "select count(id) from web_user_collect where 1=1 ";
    String where = "";
    if (!(form.getParam().size() == 0)) {
      where = " and userId=:userId";
    }
    sql += where;
    countSql += where;
    return get(sql, countSql, form, form.getParam());
  }


  /**
   * 删除接口
   *
   * @param form 删除表单
   * @return
   */
  @Override
  public Result delete(DeleteForm form) {
    this.baseDao.delete(WebUserCollect.class, form.getId());
    return new Result().setResult(true);
  }

  /**
   * 更新或者保存
   *
   * @param baseForm 更新表单
   * @return
   * @throws InvocationTargetException
   * @throws IllegalAccessException
   */
  @Override
  public Result saveOrUpdate(BaseForm baseForm) throws InvocationTargetException, IllegalAccessException {
    Result result = new Result();
    WebUserCollect collect = (WebUserCollect) baseForm;
    WebUserCollect collect1 = (WebUserCollect) this.baseDao.get(WebUserCollect.class, collect.getId());
    if (collect1 == null) {
      // 新增操作
      collect.setCreateDate(new Date());
      this.baseDao.saveOrUpdate(collect);
      return result.setResult(true);
    } else {
      // 更新操作
      this.baseDao.merge(collect);
      return result.setResult(true);
    }
  }
}
